#include "GUI_core.h"
#include "UI_Menu_Index.h"
#include "GUI_Touch.h"
extern Current_Menu_t Current_menu;
extern Touch_Stack_t  Touch_Stack;  //全局触摸栈


//显示缓冲区
//uint8_t displaybuf1[LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT]; //主缓存区
uint8_t displaybuf2[LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT]; //备份缓存区
//整屏幕每一行缓冲
uint8_t LCD_Display_Buf[LCD_GUI_HEIGHT][LCD_GUI_WIDTH*2]={0};




void LCD_Draw_Image(uint16_t xstart,uint16_t ystart,uint16_t xend,uint16_t yend,uint32_t address)
{   


  uint32_t  length=0;

  uint32_t Address=0;
  Image_Information_t image;
  uint8_t buf[8]={0};

  qspi_flash_read( address, 8,buf);
  //for(int i=0;i<8;i++)
  //printf("buf[%d]:%x",i,buf[i]);


  image.Width= (buf[0]<<8)|buf[1];//获取图片宽度
  image.Height=(buf[2]<<8)|buf[3];//获取图片高度
  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
#if 0   //打印图片信息地址
  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
#endif
  //printf("\r\n");
  //printf("\r\n");
  //printf("\r\n");
  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
  //printf("address=%x\r\n",address);

  length=image.Width*image.Height*2;
  uint8_t buffer[length];

  Address=image.Address;

      //printf("Image Address=%d\r\n",Address);
 qspi_flash_read(Address,length,buffer);
 st7796h_draw(xstart, ystart, image.Width,image.Height, buffer);
}


void Clear_LCD_Display_Buf1()
{
    //memset(displaybuf1,0,sizeof(displaybuf1));
}
void Clear_LCD_Display_Buf2()
{
    memset(displaybuf2,0,sizeof(displaybuf2));
}

uint16_t GUI_Count=0;

void GUI_Refresh_Handler()
{

#if 0
	GUI_Count++;
    LCD_Draw_Image(0,0,0,0,bg1Image_ADDRESS);
    LCD_Draw_Image(0+GUI_Count,20,0,0,zoom_shutdown_50Image_ADDRESS);
    LCD_Draw_Image(0+GUI_Count,70,0,0,zoom_skip_50Image_ADDRESS);
    LCD_Draw_Image(0+GUI_Count,120,0,0,zoom_sleep_50Image_ADDRESS);
    LCD_Draw_Image(0+GUI_Count,170,0,0,zoom_stopwatch_50Image_ADDRESS);
    LCD_Draw_Image(0+GUI_Count,120,0,0,zoom_swim_50Image_ADDRESS);
#endif

	 //显示处理函数
     Menu_Display_Handler_Process(Current_menu.current_menu);
	 //更新屏幕缓冲区
	 //GUI_LCD_Refresh();


}



void GUI_Touch_Handler()
{

#if 0
	 if(Touch_is_pressed())
	 {
		 Menu_Touch_Handler_Process(Current_menu.current_menu);
	 }
#else
	// printf("---------------------%s\r\n",__FUNCTION__);
  if(Touch_Status_Stack_Is_Full(&Touch_Stack)==true)
  {
	//printf("---------------------Touch_Status_Stack_data_Anslaysic------------------------------\r\n");
    Touch_Status_Stack_data_Anslaysic(&Touch_Stack);

	Menu_Touch_Handler_Process(Current_menu.current_menu);//执行相应的触摸处理函数
    Touch_Status_Stack_Init();
  }
#endif
}




void GUI_Horizon_Scroll(uint16_t xoffset,Move_Dir_t dir)  //水平滚动
{
  //printf("%s   dir=%d\r\n",__FUNCTION__,dir);
	uint16_t x=0,y=0;
   if((dir!=Move_Left)&&(dir!=Move_Right))
   	{
   	 //printf("Move_Dir_t is not true\r\n");
      return ;
   	}
 
#if 0
    //修改第二屏显示缓冲数据
    for(int y=0;y<LCD_GUI_HEIGHT;y++)
    //控制每行数据
    {


		  for(int i=0;i<LCD_GUI_WIDTH;i++)
		  {
			displaybuf2[y*(xoffset)*2] =   displaybuf2[y*i*2];
			displaybuf2[y*(xoffset)*2+1] = displaybuf2[y*i*2+1];
		  }
    }
#else

   // printf("for Init  \n");
    for( y=0;y<LCD_GUI_HEIGHT;y++)
    {
   	 //printf("%d\r\n",y);
      for( x=0;x<LCD_GUI_WIDTH;x++)
      {
     	 //printf("%d\r\n",x);


#if 0
		 displaybuf2[(y*LCD_GUI_WIDTH*2)-(x*2)  -xoffset*2]= displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2)  -xoffset*2];
		 displaybuf2[(y*LCD_GUI_WIDTH*2)-(x*2+1)-xoffset*2]= displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2+1)-xoffset*2];
#endif

if((dir==Move_Left))
{
	 if(x<xoffset)
				  continue;

		 displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2)  -xoffset*2] =  displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2)];
		 displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2+1)-xoffset*2] =  displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2+1)];
}else if(dir==Move_Right)
{
	 if(x<xoffset)
				  continue;

	 displaybuf2[(y+1)*LCD_GUI_WIDTH*2 -(y*LCD_GUI_WIDTH*2)+(x*2)  +xoffset*2] =  displaybuf2[(y+1)*LCD_GUI_WIDTH*2  -(y*LCD_GUI_WIDTH*2)+(x*2)];
	 displaybuf2[(y+1)*LCD_GUI_WIDTH*2 -(y*LCD_GUI_WIDTH*2)+(x*2+1)+xoffset*2] =  displaybuf2[(y+1)*LCD_GUI_WIDTH*2  -(y*LCD_GUI_WIDTH*2)+(x*2+1)];
}


  		//printf("[%d],", displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2)  ] );
  		//printf("[%d],", displaybuf2[(y*LCD_GUI_WIDTH*2)+(x*2+1)] );
      }
  		//printf("\r\n");
    }

    //printf("for exit\r\n");
#endif

  //更新显示缓冲区
  //memcpy(displaybuf1,displaybuf2,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
  //printf("function exit\r\n");
}



void GUI_Vertical_Scroll(uint16_t yoffset)  //垂直滚动
{

	  for(int i=0;i<LCD_GUI_HEIGHT;i++)
	  {


	  }


}



void GUI_Write_LCD_Display_Buf(uint16_t xstart,uint16_t ystart,uint16_t xend,uint16_t yend,uint32_t address,uint8_t *display_buf)
{
	  uint32_t  length=0;
	  uint32_t Address=0;
	  Image_Information_t image;
	  uint8_t buf[9]={0};
	  qspi_flash_read(address, 9,buf); //读取图片信息
	  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
	  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
	  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	  image.Alpha=buf[8]; //获取当前图片是否为透明属性



	  
	#if 0   //打印图片信息地址
	  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x,image.Alpha=%d\r\n",image.Width,image.Height,image.Address,image.Alpha);
	#endif
	  //printf("\r\n");
	  //printf("\r\n");
	  //printf("\r\n");
	  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
	  //printf("address=%x\r\n",address);
	  length=image.Width*image.Height*2;//获取图片大小

	  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
	  {
		  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
	  }

	  //uint8_t buffer[length];
	  Address=image.Address;
	  //printf("Image Address=%d\r\n",Address);
	  qspi_flash_read(Address,length,display_buf);

	  //更新显示缓冲区  备份缓冲区
	  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1

}





void GUI_Write_LCD_Display_Buf_Part(uint16_t xstart,uint16_t ystart,uint16_t xend,uint16_t yend,uint32_t address,uint8_t *display_buf,uint16_t ypos,uint16_t yoffset)
{
	  uint32_t  length=0;
	  uint32_t Address=0;
	  Image_Information_t image;
	  uint8_t buf[8]={0};
	  qspi_flash_read(address, 8,buf); //读取图片信息
	  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
	  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
	  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	#if 0   //打印图片信息地址
	  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
	  //printf("\r\n");
	  //printf("\r\n");
	  //printf("\r\n");
	  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
	  //printf("address=%x\r\n",address);
	  length=image.Width*ypos*2;//获取图片大小

	  if(image.Width*ypos*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
	  {
		  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
	  }

	  //uint8_t buffer[length];
	  Address=image.Address+yoffset*ypos*image.Width*2;
	  //printf("Image Address=%d\r\n",Address);
	  qspi_flash_read(Address,length,display_buf);

	  //更新显示缓冲区  备份缓冲区
	  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1

}













//水平滚动
void LCD_Horizon_Scroll_Display()
{
//#define LCD_GUI_WIDTH         240     //屏幕宽度
//#define LCD_GUI_HEIGHT        240     //屏幕高度



   for(int y=0;y<LCD_GUI_HEIGHT;y++)
   		for(int x=0;x<LCD_GUI_WIDTH;x++)
   		{
   			LCD_Display_Buf[y][x];
   		}
}


uint8_t  Vertical_Scroll_Count=0;
uint8_t  Vertical_Down_Scroll_Count=240/20;

//垂直滚动
void LCD_Vertical_Scroll_Display(Move_Dir_t dir,uint16_t yoffset)
{

	        uint32_t length=0;
			uint32_t Address=0;
			Image_Information_t image;
			uint8_t buf[8]={0};



	if((dir!=Move_Up)&&(dir!=Move_Down))
	 {
	  //printf("Move_Dir_t is not true\r\n");
	   return ;
	 }

  	 
	 if(dir==Move_Up)      //向上滚动
	 {

	 #if 0
		memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1
	 		for(int y=1;y<LCD_GUI_HEIGHT;y++)

		     memcpy((&LCD_Display_Buf[y-1]),(&LCD_Display_Buf[y]),LCD_GUI_WIDTH*2);

	 	{

			        uint32_t length=0;
					uint32_t Address=0;
					Image_Information_t image;
					uint8_t buf[8]={0};
					qspi_flash_read(cat3Image_ADDRESS, 8,buf); //读取图片信息
					image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
					image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
					image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
					#if 0   //打印图片信息地址
					printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
					#endif
					length=image.Width*image.Height*2;//获取图片大小
			  
					if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
					{
						length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
					}
			  
					//uint8_t buffer[length];
					Address=image.Address;
					//printf("Image Address=%d\r\n",Address);
					qspi_flash_read(Address+yoffset*(image.Width*2),image.Width*2,&LCD_Display_Buf[LCD_GUI_HEIGHT-1]);
			        //yoffset++;
					//更新显示缓冲区  备份缓冲区
					// memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
		            //}
	 			}
			  memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1



#else
	 memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

		 for(int y=1;y<LCD_GUI_HEIGHT/yoffset;y++)
		  memcpy((&LCD_Display_Buf[(y-1)*yoffset]),(&LCD_Display_Buf[y*yoffset]),LCD_GUI_WIDTH*2*yoffset);
	 {
				 qspi_flash_read(cat3Image_ADDRESS, 8,buf); //读取图片信息
				 image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
				 image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
				 image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
			#if 0	//打印图片信息地址
				 printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
			#endif
				 length=image.Width*image.Height*2;//获取图片大小
		   
				 if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
				 {
					 length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
				 }
		   
				 //uint8_t buffer[length];
				 Address=image.Address;
				 //printf("Image Address=%d\r\n",Address);
				 qspi_flash_read(Address+(Vertical_Scroll_Count*yoffset)*(image.Width*2),image.Width*2*yoffset,&LCD_Display_Buf[LCD_GUI_HEIGHT-yoffset-1]);
				 //yoffset++;
				 //更新显示缓冲区  备份缓冲区
				 // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
				 //}

		           Vertical_Scroll_Count++;
		  
					if((LCD_GUI_HEIGHT/yoffset )==Vertical_Scroll_Count)
					{
					  Vertical_Scroll_Count=0;
					}
             printf("-------------------------------------Vertical_Scroll_Count=%d\r\n",Vertical_Scroll_Count);
		  }
		   memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1
	      

#endif
	 }
 	else if(dir==Move_Down)//向下滚动
 	{
#if 1

	
	memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1
		for(int y=(LCD_GUI_HEIGHT/yoffset-1);y>0;y--)
		{
		 memcpy((&LCD_Display_Buf[y*yoffset]),(&LCD_Display_Buf[(y-1)*yoffset]),LCD_GUI_WIDTH*2*yoffset);
		// printf("------------------------------------y=%d\r\n",y);
		}
	{
	
				//uint32_t length=0;
				//uint32_t Address=0;
				//Image_Information_t image;
				//uint8_t buf[8]={0};
				qspi_flash_read(cat3Image_ADDRESS, 8,buf); //读取图片信息
				image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
				image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
				image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	   #if 0   //打印图片信息地址
				printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	   #endif
				length=image.Width*image.Height*2;//获取图片大小
		  
				if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
				{
					length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
				}
		  
				//uint8_t buffer[length];
				Address=image.Address;
				//printf("Image Address=%d\r\n",Address);
				qspi_flash_read(Address+(Vertical_Down_Scroll_Count*yoffset)*(image.Width*2),image.Width*2*yoffset,&LCD_Display_Buf[0]);
				//yoffset++;
				//更新显示缓冲区  备份缓冲区
				// memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
				//}
	
				Vertical_Down_Scroll_Count--;
		 
				   if(((Vertical_Down_Scroll_Count))==0)
				   {
				  	 Vertical_Down_Scroll_Count=LCD_GUI_HEIGHT/yoffset;
				   }
			//printf("-------------------------------------Vertical_Scroll_Count=%d\r\n",Vertical_Down_Scroll_Count);
		 }
		  memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1




    #else

    printf("--------------------------Move_Down\r\n");

	#endif



	}




}







void GUI_LCD_Refresh() //GUI界面更新函数
{
	st7796h_draw(0, 0, LCD_GUI_WIDTH,LCD_GUI_HEIGHT, displaybuf2);
}


